/**
 * 缓存工具类
 */
var dtime = '_deadtime'
export function put (k, v, t) {
  setStorageSync(k, v)
  if (t) {
    var seconds = parseInt(t)
    if (seconds > 0) {
      var timestamp = new Date().getTime()
      timestamp = parseInt(timestamp / 1000 + seconds)
      setStorageSync(k + dtime, timestamp + '')
    } else {
      removeStorageSync(k + dtime)
    }
  }
}
export function get (k, def) {
  var deadtime = parseInt(getStorageSync(k + dtime))
  if (deadtime) {
    if (parseInt(deadtime) < (new Date().getTime()) / 1000) {
      if (def) { return def } else { return }
    }
  }
  var res = getStorageSync(k)
  if (res) {
    return res
  } else {
    return def
  }
}

export function remove (k) {
  removeStorageSync(k)
  removeStorageSync(k + dtime)
}
export function clear () {
  sessionStorage.clear()
}

function setStorageSync (k, v) {
  sessionStorage.setItem(k, v)
}
function removeStorageSync (k) {
  sessionStorage.removeItem(k)
}
function getStorageSync (k) {
  return sessionStorage.getItem(k)
}
export default {
  put,
  get,
  remove,
  clear
}
